package controller;

import java.util.ArrayList;
import java.util.List;

import controller.Protocol.ClientCommands;


/**
 * this is the main class for the network controler
 * @author Jose Luis Garcia && Alvaro Blazquez Checa
 *
 */
public class Controller {
	
	private NetworkHandler networkHandler;
	
	/**
	 * this is a NO param constructor 
	 */
	public Controller() {
	}
		
	/**
	 * this will set the networkHandler
	 * @param networkHandler
	 */
	public void setNetworkHandler(NetworkHandler networkHandler) {
		this.networkHandler = networkHandler;
	}


	/**
	 * this is the listen method
	 */
	public void listen(){
		
	}
	
	/**
	 * this is the message for server to reach a move 
	 * @param square
	 */
	public void movePlayer(int square){
		
		ClientMessage message = new ClientMessage(ClientCommands.MY_MOVE);
		List<String> info= new ArrayList<String>();
		info.add(String.valueOf(square));
		message.setInfo(info);
		networkHandler.sendMessage(message);
		
	}
	
	
	/**
	 * this is a message to server to let it know that the player has end his turn 
	 */
	public void endTurn() {
		
		ClientMessage message = new ClientMessage(ClientCommands.END_TURN);
		networkHandler.sendMessage(message);
		
	}
	
	/**
	 * this is a message to server to let it know the player info 
	 */
	public void takePlayerInfo(List<String> info) {
		
		ClientMessage message = new ClientMessage(ClientCommands.TAKE_PLAYER_INFO);
		message.setInfo(info);
		networkHandler.sendMessage(message);
		
	}
	
	/**
	 * this is a message to server to let it know that the player has 
 	 * write in the chat
	 */
	public void sendChatMessage(String chatMessage) {

		if(networkHandler.connected) {

			ClientMessage message = new ClientMessage(ClientCommands.CHAT);
			List<String> info = new ArrayList<String>();
			info.add(chatMessage);
			message.setInfo(info);
			networkHandler.sendMessage(message);

		}
		
	}
	
	/**
	 * this is a message to server to let it know that the player wants to 
	 * meeting with other player
	 * @param playerName
	 */
	public void askPlayerForQuestionMeeting(String playerName) {
		
		if(networkHandler.connected) {

			ClientMessage message = new ClientMessage(ClientCommands.ASK_PLAYER);
			List<String> info = new ArrayList<String>();
			info.add(playerName);
			message.setInfo(info);
			networkHandler.sendMessage(message);

		}
		
	}
	
	/**
	 * 	
	 * this is a message to server to let it know that the player has answered
	 * his proposal
	 * @param answer
	 */
	public void answerPlayerForQuestionMeeting(String answer) {
		
		if(networkHandler.connected) {

			ClientMessage message = new ClientMessage(ClientCommands.ANSWER_PLAYER);
			List<String> info = new ArrayList<String>();
			info.add(answer);
			message.setInfo(info);
			networkHandler.sendMessage(message);

		}
		
	}

	
	/**
	 *
	 * this is a message to server to let it know that the player has send a question 
	 * @param list
	 */
	public void sendQuestion(List<String> list) {
		

		if(networkHandler.connected) {

			ClientMessage message = new ClientMessage(ClientCommands.QUESTION_SEND);
			message.setInfo(list);
			networkHandler.sendMessage(message);

		}
		
	}

	/**
	 * this is a message to server to let it know that the player has send a revelation
	 * @param list
	 */
	public void sendRevelation(List<String> list) {
		
		if(networkHandler.connected) {

			ClientMessage message = new ClientMessage(ClientCommands.SEND_REVELATION);
			message.setInfo(list);
			networkHandler.sendMessage(message);

		}
		
	}
	
	/**
	 * this is a message to server to let it know that the player has play a card
	 * @param cardName
	 * @param feature
	 */	
	public void playCard(String cardName,String feature,String player1,String player2) {
		
		if(networkHandler.connected) {

			ClientMessage message = new ClientMessage(ClientCommands.PLAY_CARD);
			List<String> info = new ArrayList<String>();
			info.add(cardName);
			info.add(feature);
			info.add(player1);
			info.add(player2);
			message.setInfo(info);
			networkHandler.sendMessage(message);

		}
	}
	
}
